package com.lionel.singleton;

import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 714936489@qq.com
 * @date 2018/5/17 20:59
 */
@Slf4j
public class SingletonExample3{
    private static AtomicInteger count = new AtomicInteger(0);
    private SingletonExample3() {
        log.info("私有构造函数被实例化:{}次",count.incrementAndGet());
    }
    private static SingletonExample3 instance = null;
    public static synchronized SingletonExample3  getInstance(){
        if(instance==null){
            instance = new SingletonExample3();
        }
        return instance;
    }
}
@Slf4j
class SingletonExample3Tester{
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for(int i = 0;i<20;i++){
            executorService.execute(()->{
                log.info("ThreadName:{},Object:{}",
                        Thread.currentThread().getName(),SingletonExample3.getInstance());
            });
        }
        executorService.shutdown();
    }
}

